<?php

function setbettype1_0($lotto_id,$gametype,$bettype,$subid,$numArr,$autoRateArr,$RateConfig_sb,$SRate_sb,$betname_sb,$fieldArr,$addVal)
{
    $fieldArr['method'] = 6;
    $num = current($numArr);
    switch ( $bettype )
    {

        default :
            return false;
    }
    $val = 0;
    $valIdx = "{$val}_{$subid}";
    if ( ! isset($autoRateArr[$val]) || $autoRateArr[$val]['money'] <= 0 )
    {
        return;
    }
    $rateArr = $RateConfig_sb->setRateArrDir($numRateArr,$addVal);
    $rateOldArr = array();
    $rateNewArr = array();
    $rateOldArr[0] = $SRate_sb->getRates($lotto_id,0,1,$val,$subid);
    if ( 0 < count($rateArr) )
    {
        $okFlag = $SRate_sb->updateRateByMltVal($lotto_id,$val,$subid,$rateArr);
    }
    $rateNewArr[0] = $SRate_sb->getRates($lotto_id,0,1,$val,$subid);
    foreach ( $numRateArr as $newVal )
    {
        $newKey = "r" . $newVal;
        foreach ( $rateNewArr as $rateNewArrKey=> $rateNewArrVal )
        {
            $rateOld = $rateOldArr[$rateNewArrKey];
            $rateNew = $rateNewArr[$rateNewArrKey];
            if ( $rateOld[$newKey] != $rateNew[$newKey] )
            {
                $fieldArr['betcontent'] = "{$RateConfig_sb->xyidTitle[$rateNewArrKey]}[{$betname_sb[$valIdx]}]" . $RateConfig_sb->getBetContent($val,$newVal);
                $fieldArr['orate'] = $rateOld[$newKey];
                $fieldArr['nrate'] = $rateNew[$newKey];
                setRateRecord($fieldArr);
            }
        }
    }
}

if ( ! defined("IN_OP") )
{
    exit("You can't access this file directly...");
}
//if ( $curruser['prev_id'] != 0 )
//{
//    exit();
//}
require_once( "class_3d/CheckMode_3d.php" );
$gametype = intval(getvar("gametype"));
$chkGametype = chkGametype($gametype);
if ( $chkGametype != 4 )
{
    exit("error");
}
$chk = new CheckMode_3d($curruser['acl'],5,$gametype);
$chk->checkShow3dPage();
$betname_sb = judge_type($gametype);
unset($chk);
if ( $curruser['acl'] != "255" && $curruser['acl'] != "300" )
{
    exit();
}
require( "class_sb/SRate_sb.php" );
require( "class_sb/RateConfig_sb.php" );
require( "class_sb/AutoRateConfig_sb.php" );
$conf = new Config();
$SRate_sb = new SRate_sb($gametype);
$RateConfig_sb = new RateConfig_sb($gametype);
$AutoRateConfig_sb = new AutoRateConfig_sb($betname_sb,$g_autorate_sb[$gametype],$gametype);

$tbl = $g_gametype_3d[$gametype]['en'];
$disk = "a";
$playtype = getvar("playtype");
$order = "0";
$split = "1";
$ratemod = "0";
$lotto_id = intval(getvar("lotto_id_3d"));
$bettype = getvar("actionBettype");
$numberStr = getvar("actionNum");
$mp = getvar("actionMP");
$addVal = floatval(getvar("addVal"));
$addValType = 1;
$addType1Val = floatval(getvar("addType1Val"));
$method = intval(getvar("method"));
$pagename = intval(getvar("pagename"));
if ( $mp == "m" )
{
    $addVal = 0 - $addVal;
}
$errorMsg = "";
if ( $curruser['enabled'] == 0 || $curruser['enabled'] == 2 )
{
    exit();
}
else if ( $playtype == "" || ! isset($RateConfig_sb->playGroupArr02[$playtype]) )
{
    echo "playtype:{$playtype}";
    exit();
}
$bettypeArr = explode(",",$bettype);
$numArr = explode(",",$numberStr);
$bettype1_0 = intval($conf->get("bettype1_0_{$tbl}"));
$chkBet1_0Arr = array(1,2,3,4,5,6);
$xyMode = 0;
$bettype1_0_sb = intval($conf->get("bettype1_0_{$tbl}"));
$autoRateArr = $AutoRateConfig_sb->getAutoRateAll();
$disChange = array();
if ( $addValType == 0 )
{
    $addVal = $addType1Val;
}
$sql = "SELECT round, open FROM lotto_{$tbl} WHERE id = {$lotto_id} LIMIT 1";
$rs = getdb($sql);
if ( $rs->eof )
{
    exit();
}
$fieldArr = array("method"=>$method,"gametype"=>$gametype,"round"=>$rs->f['round'],
    "lotto_id"=>$lotto_id,"betcontent"=>"","orate"=>"","nrate"=>"","pagename"=>"0",
    "paperopen"=>$rs->f['open']);
$rs->close();
$okFlag = true;
$ratePec = 1;
$chkSynBallFlag = 20 < $gametype ? intval(getvar("chkSynBall18")) : 0;
foreach ( $bettypeArr as $valStr )
{
    $valArr = explode("_",$valStr);
    if ( count($valArr) != 2 )
    {
        $okFlag = false;
        break;
    }
    $val = intval($valArr[0]);
    $mysubid = intval($valArr[1]);
    if ( ! in_array($val,$RateConfig_sb->playGroupArr02[$playtype]) )
    {
        $okFlag = false;
        break;
    }
    if ( ! isset($autoRateArr[$val]) || $autoRateArr[$val]['money'] <= 0 )
    {
        array_push($disChange,$val);
        continue;
    }
    $betTypeNumberArr = $RateConfig_sb->getBetTypeNumber($val);
    $numRateArr = array_intersect($betTypeNumberArr,$numArr);
    $rateArr = $RateConfig_sb->setRateArrDir($numRateArr,$addVal);
    if ( $chkSynBallFlag )
    {
        $subidArr = array(1,2,3,4,5,6,7,8);
        $fieldArr['method'] = "1{$method}07";
    }
    else
    {
        $subidArr = array($mysubid);
        $fieldArr['method'] = $method;
    }
    foreach ( $subidArr as $subid )
    {
        $valIdx = "{$val}_{$subid}";
        $rateOldArr = array();
        $rateNewArr = array();
        $rateOldArr[0] = $SRate_sb->getRates($lotto_id,0,1,$val,$subid);
        if ( 0 < count($rateArr) )
        {
            $okFlag = $SRate_sb->updateRateByAddVal($lotto_id,$val,$subid,$rateArr);
        }
        $rateNewArr[0] = $SRate_sb->getRates($lotto_id,0,1,$val,$subid);
        $bettype1_0Rate = 0 - 1;
        foreach ( $numRateArr as $newKey=> $newVal )
        {
            foreach ( $rateNewArr as $rateNewArrKey=> $rateNewArrVal )
            {
                $rateOld = $rateOldArr[$rateNewArrKey];
                $rateNew = $rateNewArr[$rateNewArrKey];
                if ( $rateOld[$newKey] != $rateNew[$newKey] )
                {
                    $fieldArr['betcontent'] = "{$RateConfig_sb->xyidTitle[$rateNewArrKey]}[{$betname_sb[$valIdx]}]" . $RateConfig_sb->getBetContent($val,$newVal);
                    $fieldArr['orate'] = $rateOld[$newKey];
                    $fieldArr['nrate'] = $rateNew[$newKey];
                    setRateRecord($fieldArr);
                }
                if ( $rateOld[$newKey] != 0 )
                {
                    $bettype1_0Rate = $rateNew[$newKey] / $rateOld[$newKey];
                }
                else
                {
                    $bettype1_0Rate = 1;
                }
            }
        }
    }
    if ( $bettype1_0 && in_array($val,$chkBet1_0Arr) && count($numRateArr) == 1 && 0 < $bettype1_0Rate )
    {
        setbettype1_0($lotto_id,$gametype,$val,$mysubid,$numRateArr,$autoRateArr,$RateConfig_sb,$SRate_sb,$betname_sb,$fieldArr,$bettype1_0Rate);
    }
}
if ( $okFlag )
{
    $disChangeStr = 0 < count($disChange) ? "\"" . implode("\",\"",$disChange) . "\"" : "";
    header("Cache-Control: no-cache, must-revalidate");
    header("Content-Type: text/plain");
    echo "{";
    echo "\"msg\":\"OK\"";
    echo ",\"disChange\":[" . $disChangeStr . "]";
    echo ",\"chgMsg1\":[]";
    echo ",\"chgMsg2\":[]";
    echo "}";
}
else
{
    header("Cache-Control: no-cache, must-revalidate");
    header("Content-Type: text/plain");
    echo "{\"msg\":\"error\"}";
}
exit();